<?php
namespace Home\Controller;
use Think\Controller;
Class ReleaseController extends Controller
{
    //我的发布
    public function home_release()
    {
        $token = 'muchen';//trim(I("post."));

        if (empty($token)) {

            //$this->error("请登录!",U("redirect_login"));

            $info['code'] = 3;

            $info['msg']  = "请登录";

            echo json_encode($info);die;

        }

        $user_token['user_token'] = $token;

        $get_user_info = D("Index")->get_user_one_info($user_token);

		$info['code'] = 0;
		
		$info['msg']  = "成功";
				
		if ($get_user_info['user_open'] != 1) {
			$info['code'] = 1;
			
			$info['msg']  = "您的店铺未开启";
			
			echo json_encode($info);die;
		}

        $condition["release_state"]   = 1
        ;
        $condition["release_user_id"] = $get_user_info['user_id'];

        $release = D("Release")->get_release_info($condition,"release_updated_time desc");

		if (!$release) {
			$info['code'] = 2;
		
			$info['msg']  = "暂时没有发布数据";
			
			echo json_encode($info);die;

		}
		$info['data'] = $release;

		echo json_encode($info);die;
    }

    //刷新按钮操作
    public function refresh_release()
    {

        $release_id = intval(I("post.id"));

        $token = 'muchen';//trim(I("post."));

        $user_token['user_token'] = $token;

        $get_user_one_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_one_info)) {

            $info['code'] = 2;
        
            $info['msg']  = "请登录";
            
            echo json_encode($info);die;             
        }

        $condition["release_user_id"]  = $get_user_one_info['user_id'];

        $condition["release_id"]        = $release_id;

        $param['release_updated_time'] = date("Y-m-d H:i:s",time());

        $save_updated_time = D("Release")->delete_release_info($condition,$param);

            $info['code'] = 0;
        
            $info['msg']  = "刷新成功";

        if (!$save_updated_time) {
            $info['code'] = 1;
        
            $info['msg']  = "刷新失败";
        }
            echo json_encode($info);die;         
        }

    //发布信息
    public function release()
    {
        $token = trim(I("post."));

        $user_token['user_token'] = $token;

        $get_user_one_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_one_info)) {

            $this->error("请登录!",U("redirect_login"));
        }
        $condition["user_id"] = $get_user_one_info['user_id'];

        $res = array();
        $res[''] = '';
        $res[''] = '';
        $res[''] = '';
        $res['release_created_time'] = date("Y-m-d H:i:s",time());
        $res['release_updated_time'] = date("Y-m-d H:i:s",time());

        $result = D("Release")->add_release_info($res,$condition);

        if ($result) {
            $this->success("发布成功",U(""));
        } else {
            return false;
        }

    }

    //修改发布信息
    public function save_release()
    {

    }

    //删除我的发布信息
    public function delete_release()
    {
        $token = 'muchen';//trim(I("post."));

        $release_id = intval(I("post.id"));

        $user_token['user_token'] = $token;

        $get_user_one_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_one_info)) {

            $info['code'] = 2;
        
            $info['msg']  = "请登录";
            
            echo json_encode($info);die;     
        }
        $condition["release_user_id"] = $get_user_one_info['user_id'];

        $condition["release_id"] = $release_id;

        $param['release_state'] = 0;

        $delete_release = D("Release")->delete_release_info($condition, $param);

        if ($delete_release) {
            $info['code'] = 0;
        
            $info['msg']  = "删除成功";
        } else {
            $info['code'] = 1;
        
            $info['msg']  = "删除失败";
        }
        echo json_encode($info);die;           
    }

    //token过期自动跳转至登陆页面
    public function redirect_login()
    {
        $this->display("Index/login");
    }


    //发布的详情
    public function release_info() 
    {
        $release_id = intval(I("post.id"));

        $condition['release_id'] = $release_id;

        $get_release_one_info = D("Release")->get_release_one_info($condition);

            $info['code'] = 0;
            
            $info['msg']  = "成功";

            $info['data']  = $get_release_one_info;

        if (!$get_release_one_info) {
            $info['code'] = 1;

            $info['msg']  = "失败";

        }
            echo json_encode($info);die;

    }


}
?>


